package it.unibz.easytravel.travel;

import it.unibz.easytravel.R;

import it.unibz.easytravel.db.DebtsDbAdapter;
import it.unibz.easytravel.db.ExpensesDbAdapter;
import it.unibz.easytravel.db.TravellersDbAdapter;
import it.unibz.easytravel.expenses.AddComplexExpenseActivity;
import it.unibz.easytravel.expenses.AddExpense;
import it.unibz.easytravel.expenses.Report;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

//This class will have to be removed in the end
//it is now supposed to be used for management of the module

public class ExpensesActivity extends Activity {

	private static int travel_id;

	private Activity activity;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.expenses_module);
	
		SharedPreferences settings = getSharedPreferences(EditTravel.PREFS_NAME, 0);
		travel_id = settings.getInt(EditTravel.TRAVEL_ID, -1);
	
		
		this.activity = this;

		Button button_expenses = (Button) findViewById(R.id.AddExpenses);
		button_expenses.setOnClickListener(mAddExpense);

		Button button_report = (Button) findViewById(R.id.ViewExpenses);
		button_report.setOnClickListener(mReportExpenses);

		Button button_delete = (Button) findViewById(R.id.DeleteExpenses);
		button_delete.setOnClickListener(mDeleteExpenses);	
		
		Button button_add_compl = (Button) findViewById(R.id.button_add_compl_exp);
		button_add_compl.setOnClickListener(mAddComplExpense);	
	}

	
	private OnClickListener mAddComplExpense = new OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent(ExpensesActivity.this, AddComplexExpenseActivity.class);
			startActivity(intent);
		}
	};

	private OnClickListener mAddExpense = new OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent(ExpensesActivity.this, AddExpense.class);
			startActivity(intent);
		}
	};

	private OnClickListener mDeleteExpenses = new OnClickListener() {
		public void onClick(View v) {
			ExpensesDbAdapter mExpensesDb = new ExpensesDbAdapter(activity);
			mExpensesDb.open();
			mExpensesDb.delete();
			mExpensesDb.close();

			TravellersDbAdapter mTravelersDb = new TravellersDbAdapter(activity);
			mTravelersDb.open();
			mTravelersDb.cleanData(travel_id+"");
			mTravelersDb.close();

			DebtsDbAdapter mDebtsDb = new DebtsDbAdapter(activity);
			mDebtsDb.open();
			mDebtsDb.delete();
			mDebtsDb.close();

			Toast.makeText(getBaseContext(), "all databases were cleared!",
					Toast.LENGTH_SHORT).show();
		}
	};

	private OnClickListener mReportExpenses = new OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent(ExpensesActivity.this, Report.class);
			startActivity(intent);
		}
	};	
}
